package io.renren.modules.mail.service.impl;

import io.renren.modules.mail.dao.PaperDao;

import io.renren.modules.mail.pojo.Paper;
import io.renren.modules.mail.service.PageService;

import io.renren.modules.mail.vo.PageObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class PageServiceImpl implements PageService {
    @Autowired(required = false)
    PaperDao paperDao;
    @Override
    public PageObject<Paper> findPageObjects(Integer pageCurrent) throws Exception {
        if (pageCurrent == null || pageCurrent < 1) {

            throw new IllegalArgumentException("当前页码不正确");
        }
        int rowCount = paperDao.getRowCount();
        //判断
        if(rowCount==0){
            throw new Exception("系统没有查到对应记录");}
        int pageSize=2;//定义pageSize
        int startIndex=(pageCurrent-1)*pageSize;//计算startIndex
        List<Paper> records = paperDao.selectAllPaper(startIndex, pageSize);
        //装数据
        PageObject<Paper> pageObject = new PageObject<>();
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setPageSize(pageSize);
        pageObject.setRowCount(rowCount);
        pageObject.setRecords(records);
        pageObject.setPageCount((rowCount-1)/pageSize+1);
        return pageObject;
    }

}
